package com.blue.wechat.invoke.spi.media;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blue.core.dict.HttpMethod;
import com.blue.wechat.cache.Wechat;
import com.blue.wechat.invoke.BaseInvoker;
import com.blue.wechat.invoke.InvokeException;
import com.blue.wechat.invoke.WechatInvoker;
import com.blue.wechat.invoke.annotation.WechatUrl;
import com.blue.wechat.invoke.model.media.ListMaterial;
import com.blue.wechat.invoke.model.media.ListMaterialParam;

/**
 * 获取素材列表
 * 
 * @author zhengj
 * @since 1.0 2017年6月9日
 */
@WechatUrl(WechatInvoker.LIST_MATERIAL)
public class ListMaterialInvoker extends BaseInvoker<ListMaterial>
{
	public ListMaterialInvoker()
	{
	}

	@Override
	protected WechatInvoker getInvoker()
	{
		return WechatInvoker.LIST_MATERIAL;
	}
	
	@Override
	protected String invoke(String url, HttpMethod method, Object post) throws Exception
	{
		if (!(post instanceof ListMaterialParam))
			throw new InvokeException("获取永久素材列表参数类型不是：" + ListMaterialParam.class.getName());
		
		ListMaterialParam param = (ListMaterialParam)post;
		JSONObject json = new JSONObject();
		json.put("type", param.getType().name().toLowerCase());
		json.put("offset", param.getOffset());
		json.put("count", param.getCount());
		String resp = httpInvoker.post(url, json.toJSONString());
		return resp;
	}

	@Override
	protected ListMaterial handleSuccess(String resp, Wechat wechat)
	{
		ListMaterial list = JSON.parseObject(resp, ListMaterial.class);
		return list;
	}
}
